# 特征工程
import numpy as np
import pandas as pd

# 1、读入数据
trian  = pd.read_csv("E:/VC_project/data/day.csv")
print(trian.info())

# 2、对类别型特征编码
categorical_features = ["season","mnth","weathersit","weekday"]
# 将数据转换成object类型
for i in categorical_features:
    trian[i] = trian[i].astype("object")
X_train_cat = trian[categorical_features]
# 独热编码使用pandas的get_dummies方法（哑编码）
X_train_cat = pd.get_dummies(X_train_cat)
print(X_train_cat.head())

# 3、对数值型特征标准化/MinMax处理
# 由于数值型特征已经被处理过，可以使用MinMax再处理
from sklearn.preprocessing import MinMaxScaler
mms = MinMaxScaler()
numberical_features = ["temp","atemp","hum","windspeed"]
temp = mms.fit_transform(trian[numberical_features])
X_train_num = pd.DataFrame(data=temp,columns=numberical_features,index=trian.index)
print(X_train_num.head())

# 将处理后特征添加至X_train中
X_train = pd.concat([X_train_cat,X_train_num,trian["holiday"],trian["workingday"]], axis=1, ignore_index=False)

# 将特征保存到新文件中
FE_train = pd.concat([trian["instant"],X_train,trian["yr"],trian["cnt"]],axis=1)
FE_train.to_csv("E:/VC_project/data/FE_day.csv",index = False)
print(FE_train.info())

